package leecoke;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Test970 {

    public static void main(String[] args) {
        List<Integer> integers = powerfulIntegers(2, 3, 10);
        print(integers);
    }
    static void print(List<Integer> list) {
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + " ");
        }
    }

    public static List<Integer> powerfulIntegers(int x, int y, int bound) {
        List<Integer> list = new ArrayList<>();
        Queue<Integer> list1 = new LinkedList<>();
        Queue<Integer> list2 = new LinkedList<>();
        Queue<Integer> temp = new LinkedList<>();
        int numx = 1;
        while(numx <= bound){
            list1.offer(numx);
            numx *= x;
        }
        int numy = 1;
        while(numy <= bound){
            list2.offer(numy);
            numy *= y;
        }
        int sum = 0;
        while(!list1.isEmpty()){
            int num1 = list1.poll();
            temp = list2;
            while(!temp.isEmpty()){
                int num2 =  temp.poll();
                sum = num1 + num2;
                if(sum <= bound){
                    list.add(sum);
                }
            }
        }
        return list;
    }
}
